package com.xiaoge.libraryuse.recycler;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.xiaoge.basedevlibrary.base.GuoBaseActivity;
import com.xiaoge.basedevlibrary.http.GuoBaseResponseBean;
import com.xiaoge.basedevlibrary.http.HttpUtils;
import com.xiaoge.basedevlibrary.utils.AppUtil;
import com.xiaoge.basedevlibrary.utils.DpUtils;
import com.xiaoge.basedevlibrary.utils.ToastUtil;
import com.xiaoge.libraryuse.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerActivity extends GuoBaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, HttpUtils.NetResponseListener {

    // 分类数据: http://gank.io/api/data/数据类型/请求个数/第几页
    int pages = 1;
    String url = "";

    private EasyRecyclerView mRecyclerView;
    private RecyclerArrayAdapter<HttpImageBean.ResultsBean> adapter;
    private List<HttpImageBean.ResultsBean> mImages = new ArrayList();
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        mRecyclerView = (EasyRecyclerView) findViewById(R.id.recyclerView);
        url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/" + pages;

        // 设置分割线
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // GridLayoutManager layoutManager2 = new GridLayoutManager(this, 2);

        mRecyclerView.setLayoutManager(layoutManager);
        DividerDecoration itemDecoration = new DividerDecoration(Color.WHITE, DpUtils.dp2px(this, 8), DpUtils.dp2px(this, 0.5f), DpUtils.dp2px(this, 0.5f));
        itemDecoration.setDrawLastItem(false);
        mRecyclerView.addItemDecoration(itemDecoration);

        initAdapter();
    }

    private void initAdapter() {

        adapter = new RecyclerArrayAdapter<HttpImageBean.ResultsBean>(this, mImages) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MeiZiViewHolder(parent , RecyclerActivity.this);
            }
        };

        mRecyclerView.setAdapterWithProgress(adapter);

        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);


        adapter.setOnItemLongClickListener(new RecyclerArrayAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                adapter.remove(position);
                return true;
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // TODO 条目点击事件待做
                Toast.makeText(AppUtil.getContext(), "点击了第" + (position + 1) + "个元素", Toast.LENGTH_LONG).show();
            }
        });

        mRecyclerView.setRefreshListener(RecyclerActivity.this);
        onRefresh();
    }

    @Override
    public void onLoadMore() {
        pages++;
        url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/" + pages;

        // 请求太快 手动延时
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HttpUtils.getInstance().get(url, null, 200, HttpImageBean.class, RecyclerActivity.this);
            }
        }, 2000);

//        System.out.println(url + "==================");
    }

    @Override
    public void onRefresh() {
        // 将需要初始化的数据进行初始化
        pages = 1;
        adapter.clear();
        // 初始化数据集合
        mImages.clear();
        url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/" + pages;
        HttpUtils.getInstance().get(url, null, 100, HttpImageBean.class, RecyclerActivity.this);
    }

    @Override
    public void onSuccess(GuoBaseResponseBean response, int requestCode) {

        HttpImageBean bean = (HttpImageBean) response;
        if (requestCode == 100) {
            adapter.addAll(bean.getResults());
        } else if (requestCode == 200) {
            adapter.addAll(bean.getResults());
        }
    }

    @Override
    public void onFailed(int requestCode) {
        ToastUtil.showToast(AppUtil.getContext(), "请求接口失败");
    }
}
